package org.example.Server.service.sruvey.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.Server.mapper.paper.PaperPersonalMapper;
import org.example.Server.mapper.question.QuestionMapper;
import org.example.Server.mapper.survey.SurveySemMapper;
import org.example.Server.model.examination.qs.QsExam;
import org.example.Server.model.examination.qs.QsExamVO;
import org.example.Server.model.paper.PaperHeader;
import org.example.Server.model.paper.PaperQsIds;
import org.example.Server.model.question.Question;
import org.example.Server.model.survey.*;
import org.example.Server.service.paper.PaperPersonalService;
import org.example.Server.service.sruvey.SurveySemService;
import org.example.Server.service.sruvey.SurveyService;
import org.example.Server.utils.ResultAPI;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SurveySemServiceImpl extends ServiceImpl<SurveySemMapper, SemSurvey> implements SurveySemService {
    @Autowired
    private SurveySemMapper surveySemMapper;
    @Autowired
    PaperPersonalMapper paperPersonalMapper;
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    PaperPersonalService paperPersonalService;
    @Autowired
    SurveyService surveyService;

    @Transactional
    @Override
    public ResultAPI saveSurvey(SurveyPaperDTO surveyPaperDTO) {
        // 创建survey_store
        Survey survey = new Survey();
        Integer semId = surveyPaperDTO.getSem_id();
        Integer techId = surveyPaperDTO.getTech_id();
        // 不从问卷库选取
        if (surveyPaperDTO.getSurvey_id() == null) {
            // 创建paper
            PaperHeader paperHeader = new PaperHeader();
            BeanUtils.copyProperties(surveyPaperDTO, paperHeader);
            paperPersonalMapper.addPaperHeader(paperHeader, 0);
            //添加题型
            paperPersonalMapper.addPaperQsType(paperHeader.getPaper_id());
            BeanUtils.copyProperties(surveyPaperDTO, survey);
            survey.setPaper_id(paperHeader.getPaper_id());
            survey.setIs_store(0);
            surveyService.save(survey);
        } else {
            // 从问卷库选取
            Survey surPaper = surveyService.getById(surveyPaperDTO.getSurvey_id());
            Integer paperOldId = surPaper.getPaper_id();
            // 获取老试卷
            PaperQsIds oldPaper = paperPersonalMapper.getPaperQsIdsById(paperOldId);
            //添加试卷
            paperPersonalMapper.addPaperHeader(oldPaper.getPaperHeader(), 0);
            log.info(oldPaper.getPaperHeader().toString());
            Integer paperId = oldPaper.getPaperHeader().getPaper_id();
            paperPersonalMapper.addPaperQsType(paperId);
            //批量复制题目
            if (oldPaper.getQsIds().isEmpty()) {
                return ResultAPI.error("空试卷");
            }
            List<Question> questionList = questionMapper.getQuestionByIds(oldPaper.getQsIds());
            paperPersonalService.CopyQses(questionList, techId, paperId);
            //添加调查问卷
            BeanUtils.copyProperties(surveyPaperDTO, survey);
            survey.setPaper_id(paperId);
            survey.setIs_store(0);
            log.info(survey.toString());
            surveyService.save(survey);
        }
        SemSurvey semSurvey = new SemSurvey();
        semSurvey.setSurvey_id(Math.toIntExact(survey.getSurvey_id()));
        semSurvey.setSem_id(semId);
        this.save(semSurvey);
        return ResultAPI.success(semSurvey);
    }

    /**
     * 获取课堂问卷列表
     *
     * @param semId
     * @param status
     * @return
     */
    @Override
    public ResultAPI getSemSurveyList(Integer semId, Integer status) {
        List<SemSurveyVO> semSurveyVOS = surveySemMapper.getSemSurveyList(semId, status);
        return ResultAPI.success(semSurveyVOS);
    }

    @Override
    public ResultAPI deleteSurvey(List<Integer> ids) {
        surveySemMapper.deleteSurvey(ids);
        return ResultAPI.success();
    }

    /**
     * 获取问卷分析试题列表
     *
     * @param semId
     * @param surveyId
     * @param paperId
     * @return
     */
    @Override
    public ResultAPI getSurveyQsList(Integer semId, Integer surveyId, Integer paperId) {
        //先获取选择题的列表
        List<QsExamVO> selList = surveySemMapper.getSurveyQsSelList(semId, surveyId, paperId);
        //再获取问答题列表
        List<QsExamVO> qnaList = surveySemMapper.getSurveyQsQnaList(semId, surveyId, paperId);
        List<Object> res = new ArrayList<>();
        if (selList != null && !selList.isEmpty()) {
            res.add(selList);
        }
        if (qnaList != null && !qnaList.isEmpty()) {
            res.add(qnaList);
        }
        return ResultAPI.success(res);
    }
}
